package ar.republicagroup.gestion.view;

import ar.republicagroup.gestion.entities.BolsaCheque;
import ar.republicagroup.gestion.entities.Cheque;
import ar.republicagroup.gestion.entities.Cliente;
import ar.republicagroup.gestion.services.BolsaChequeServices;
import ar.republicagroup.gestion.services.ChequeServices;
import ar.republicagroup.gestion.services.ClienteServices;
import ar.republicagroup.gestion.util.JComboBoxModel;
import ar.republicagroup.gestion.util.SpringUtils;
import ar.republicagroup.gestion.util.messages.UIMessages;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author DIEGOALV
 */
public class modalPanelEditBolsaCheque extends javax.swing.JDialog {
    
    /* Services */
    ClienteServices clienteServices                 = (ClienteServices)SpringUtils.getBean("clienteServices");
    BolsaChequeServices bolsaChequeServices         = (BolsaChequeServices)SpringUtils.getBean("bolsaChequeServices");
    ChequeServices chequeServices                   = (ChequeServices)SpringUtils.getBean("chequeServices");
    
    /* Beans */
    private BolsaCheque bolsaCheque;
    
    public modalPanelEditBolsaCheque(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        cargarDatos();
    }

    
    private void cargarDatos(){
        comboCliente.setModel(clienteServices.combo());
    }

    public BolsaCheque getBolsaCheque() {
        return bolsaCheque;
    }

    public void setBolsaCheque(BolsaCheque bolsaCheque) {
        this.bolsaCheque = bolsaCheque;
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        comboCliente = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        btnConfirmar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lblBolsaNro = new javax.swing.JLabel();
        lblTotalCheques = new javax.swing.JLabel();
        lblClienteActual = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Modificar Bolsa de Cheques");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("Bolsa Nro:");

        jLabel2.setText("Cliente actual:");

        jLabel3.setText("Asignar a:");

        comboCliente.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N

        jLabel4.setText("Total de cheques:");

        btnConfirmar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/check.png"))); // NOI18N
        btnConfirmar.setText("Confirmar");
        btnConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/cancel.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        lblBolsaNro.setText("-");

        lblTotalCheques.setText("-");

        lblClienteActual.setText("-");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(37, 37, 37)
                                .addComponent(jLabel1))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel4)
                                .addComponent(jLabel2))
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblBolsaNro)
                            .addComponent(lblTotalCheques)
                            .addComponent(lblClienteActual)
                            .addComponent(comboCliente, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(btnConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblBolsaNro))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(lblTotalCheques))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblClienteActual))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(comboCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnConfirmar)
                    .addComponent(btnCancelar))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        mostrarDatosDeBolsaCheque();
        eliminarClienteDelCombo();
        
    }//GEN-LAST:event_formWindowOpened

    private void eliminarClienteDelCombo(){
        
        int tamanoCombo = comboCliente.getItemCount();
        
        for(int i = 0; i < tamanoCombo; i++){
            JComboBoxModel combo = (JComboBoxModel) comboCliente.getModel().getElementAt(i);
            if(Integer.parseInt(combo.getValue()) == bolsaCheque.getClienteId().getId()){
                comboCliente.removeItemAt(i);
                tamanoCombo--;
            }
        }
        
        comboCliente.repaint();
        
    }
    
    
    
    private void btnConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmarActionPerformed
            if(comboCliente.getSelectedIndex() != -1){
                JComboBoxModel combo = (JComboBoxModel)comboCliente.getSelectedItem();
                Cliente cliente      = clienteServices.get(Integer.parseInt(combo.getValue()));
                bolsaCheque.setClienteId(cliente);
                List<Cheque> chequeList = new ArrayList<>();
                
                for(Cheque cheque : bolsaCheque.getChequeList()){
                    if(cheque.getOrigen().equals(ChequeServices.ORIGEN_CAP_BSAS)){
                        cheque.setComision(cliente.getComisionBa());
                    }else{
                        cheque.setComision(cliente.getComisionInterior());
                    }
                    chequeList.add(cheque);
                }
                
                for(Cheque cheque : chequeList){
                    chequeServices.update(cheque);
                }
                
                bolsaCheque.setChequeList(chequeList);
                String mensaje = bolsaChequeServices.update(bolsaCheque);
                if(mensaje!=null){
                        JOptionPane.showMessageDialog(this, UIMessages.getInfoMessage("onCompleteUpdate"), UIMessages.getInfoMessage("defaultInfoTitle"), JOptionPane.INFORMATION_MESSAGE);
                        frmCheque.cargaTablaAfterEdit();
                        dispose();
                    } else {
                        JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("onUpdateError"), UIMessages.getErrorMessage("onUpdateError_title"), JOptionPane.ERROR_MESSAGE);
                    }
                
            }else{
                JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("requiredSelectedCliente"), UIMessages.getInfoMessage("defaultInfoTitle"), JOptionPane.INFORMATION_MESSAGE);
            }
    }//GEN-LAST:event_btnConfirmarActionPerformed

    private void mostrarDatosDeBolsaCheque(){
        lblBolsaNro.setText(String.valueOf(bolsaCheque.getId().intValue()));
        lblClienteActual.setText(String.valueOf(bolsaCheque.getClienteId().getNombre()));
        lblTotalCheques.setText(String.valueOf(bolsaCheque.getChequeList().size()));
    }
    
    
    
    
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                modalPanelEditBolsaCheque dialog = new modalPanelEditBolsaCheque(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnConfirmar;
    private javax.swing.JComboBox comboCliente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblBolsaNro;
    private javax.swing.JLabel lblClienteActual;
    private javax.swing.JLabel lblTotalCheques;
    // End of variables declaration//GEN-END:variables
}
